package  
{
	import org.flixel.*;
	/**
	 * ...
	 * @author Lachlan McInnes
	 */
	public class ExtrasEndingState extends FlxState
	{
		private var _bestEnding:FlxSprite;
		private var _goodEnding:FlxSprite;
		private var _badEnding:FlxSprite;
		
		private var _backButton:FlxButtonPlus;
		private var _backText1:FlxText;
		private var _backText2:FlxText;
		
		private var _buttonPressed:Boolean = false;
		private var _fadeSprite:FlxSprite;
		private var _fadeTimer:Number = 0.0;
		private const _fadeTimeMax:Number = 1.0;
		
		public function ExtrasEndingState(ending:uint)
		{
			switch (ending)
			{
			case 2:
				_bestEnding = new FlxSprite(0, 0, ImageFiles.endingBest);
				add(_bestEnding);
				break;
				
			case 1:
				_goodEnding = new FlxSprite(0, 0, ImageFiles.endingGood);
				add(_goodEnding);
				break;
				
			case 0:
				_badEnding = new FlxSprite(0, 0, ImageFiles.endingBad);
				add(_badEnding);
				break;
			}
			
			_backButton = new FlxButtonPlus(2, FlxG.height - 25, goToExtras);
			_backText1 = new FlxText(0, 0, 100, "Back").setFormat("PowellAntique", 15, 0xDDDDDD, "center", 0xFF000000);
			_backText2 = new FlxText(_backText1.x, _backText1.y, _backText1.width, _backText1.text).setFormat("PowellAntique", 15, 0xFFFFFF, "center", 0xFF000000);
			_backButton.loadGraphic(new FlxSprite().loadGraphic(ImageFiles.button), new FlxSprite().loadGraphic(ImageFiles.buttonHighlight));
			_backButton.setMouseOverCallback(mouseOverSound);
			_backButton.loadText(_backText1, _backText2);
			add(_backButton);
			
			_fadeSprite = new FlxSprite().createGraphic(640, 480, 0xff000000);
			_fadeSprite.alpha = 1;
			add(_fadeSprite);
		}
		
		override public function update():void 
		{
			super.update();
			
			if (_buttonPressed)
			{
				fadeOut();
			}
			else
			{
				fadeIn();
			}
		}
		
		private function goToExtras():void
		{
			if (!_buttonPressed)
			{
				FlxG.play(SoundFiles.selectSound, .75);
				_buttonPressed = true;
			}
		}
		
		private function fadeOut():void
		{
			_fadeTimer = _fadeSprite.alpha / _fadeTimeMax;
			_fadeTimer += FlxG.elapsed;
			
			if (_fadeTimer / _fadeTimeMax > _fadeSprite.alpha)
			{
				_fadeSprite.alpha = _fadeTimer / _fadeTimeMax;
			}
			
			if (_fadeTimer >= _fadeTimeMax)
			{
				FlxG.state = new ExtrasState(true);
			}
		}
		
		private function fadeIn():void
		{
			if (_fadeTimer < _fadeTimeMax && !_buttonPressed)
			{
				_fadeTimer += FlxG.elapsed;
				
				_fadeSprite.alpha = Math.abs(1 - (_fadeTimer / _fadeTimeMax));
			}
		}
		
		private function mouseOverSound():void
		{
			FlxG.play(SoundFiles.mouseOverSound, 0.75);
		}
		
	}

}